package com.example.myapplication.tack_13;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.example.myapplication.R;
import com.example.myapplication.tack_13.adapter.DepartmentAdapter;
import com.example.myapplication.tack_13.data.DeparArragneData;
import com.example.myapplication.utils.OkHttpTool;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class DepartmentActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView arragne;
    private TabLayout fenlei;
    private List<DeparArragneData.RowsDTO> listArragne;
    private List<String> listRegister;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_13_activty_department);
        init();
        exit_();
        getRegiste();
    }

    private void getRegiste() {

        for (String s : listRegister) {
            TabLayout.Tab tab = fenlei.newTab();
            tab.setText(s);
            fenlei.addTab(tab);
        }
        SortAdapter();
    }

    private void SortAdapter() {
        fenlei.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener(){

            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                if("普通挂号".equals(listRegister.get(position))){
                    getArragne();
                }else if("专家挂号".equals(listRegister.get(position))){

                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void getArragne() {
        String url = "prod-api/api/hospital/category/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, DeparArragneData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                DeparArragneData deparArragneData = (DeparArragneData)obj;
                if(deparArragneData.getCode() == 200){
                    listArragne = deparArragneData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            DepartmentAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void DepartmentAdapter() {
        DepartmentAdapter departmentAdapter = new DepartmentAdapter(listArragne, DepartmentActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(DepartmentActivity.this);
        arragne.setLayoutManager(linearLayoutManager);
        arragne.setAdapter(departmentAdapter);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DepartmentActivity.this.finish();
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        arragne = findViewById(R.id.list);
        fenlei = findViewById(R.id.fenlei);
        listRegister = new ArrayList<>();
        listRegister.add("专家挂号");
        listRegister.add("普通挂号");
    }
}